package com.annchain.og_browser.controller;


import com.annchain.og_browser.consts.Consts;
import com.annchain.og_browser.entity.OgbTransaction;
import com.annchain.og_browser.mapper.OgbTransactionMapper;
import com.annchain.og_browser.mapper.OgbTxRelationMapper;
import com.annchain.og_browser.service.OgbBlockService;
import com.annchain.og_browser.service.OgbTransactionService;
import com.annchain.og_browser.service.impl.OgbBlockServiceImpl;
import com.annchain.og_browser.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/blockchain")
@Api("交易接口")
public class OgbTransactionController {

    private static final Logger logger = LoggerFactory.getLogger(OgbBlockController.class);
    private final String CLASS_NAME = this.getClass().getSimpleName();

    private static final int HASH_LENGTH = 66;
    private static final int HEX_PREFIX_LENGTH = 2;

    @Autowired
    private OgbBlockService ogbBlockService;

    @Autowired
    private OgbTransactionService ogbTransactionService;

    @ApiOperation("返回最新交易列表")
    @GetMapping("/transactionlist")
    public JsonResponse<BasePageResponse<LatestTransactionVO>> findData(Integer pageNum, Integer pageSize) {
        BasePageResponse pageResponse = ogbTransactionService.findData(pageNum, pageSize);
        return JsonResponse.success(pageResponse).setCode(200).setMsg("成功");

    }

    @ApiOperation("根据交易哈希查询交易")
    @GetMapping("/transaction/{param}")
    public JsonResponse<Object> findTxByHash(@PathVariable("param") String param) {
        logger.info("########{}.{} begin...", CLASS_NAME, Thread.currentThread().getStackTrace()[1].getMethodName());
        logger.info("param:{}", param);

        OgbTransaction ogbTransaction = ogbTransactionService.findByHash(param);
        if (ogbTransaction != null) {
            switch (ogbTransaction.getType()) {
                case Consts.TX_TYPE_TX:
                    TransferTxVO transferTxVO = ogbTransactionService.findTransferByTxHash(param);
                    return JsonResponse.success(transferTxVO).setCode(200).setMsg("成功");
                case Consts.TX_TYPE_ARCHIVE:
                    DepositeTxVO depositeTxVO = ogbTransactionService.findDepositeByTxHash(param);
                    return JsonResponse.success(depositeTxVO).setCode(200).setMsg("成功");
                case Consts.TX_TYPE_ACTION:
                    IssueTxVO issueTxVO = ogbTransactionService.findIssueByTxHash(param);
                    return JsonResponse.success(issueTxVO).setCode(200).setMsg("成功");
            }
        }else{
            param = param.trim();
            if (param.length() == HASH_LENGTH || param.length() == HASH_LENGTH - HEX_PREFIX_LENGTH) {
                if (param.length() == HASH_LENGTH) {
                    param = param.substring(HEX_PREFIX_LENGTH);
                }
                OgbBlockVO ogbBlockVO = ogbBlockService.findByHash(param);
                if (ogbBlockVO == null) {
                    return JsonResponse.serverError();
                }

                return JsonResponse.success(ogbBlockVO).setMsg("成功");
            }
            else if (StringUtils.isNumeric(param) && param.length() < 10){
                int Height = Integer.parseInt(param);
                OgbBlockVO ogbBlockVO = ogbBlockService.findByHeight(Height);
                if (ogbBlockVO == null) {
                    return JsonResponse.serverError();
                }
                return JsonResponse.success(ogbBlockVO).setCode(200).setMsg("查询成功");
            } else {
                return JsonResponse.parameterError("输入数据有误").setCode(400);
            }
        }
        return JsonResponse.serverError();
    }

    @ApiOperation("返回区块中包含的交易")
    @GetMapping("/block/transactionlist/{param}")
    public JsonResponse<BasePageResponse<List<TransactionsInBlock>>> findTibByParam(Integer pageNum, Integer pageSize, @PathVariable String param) {
        logger.info("########{}.{} begin...", CLASS_NAME, Thread.currentThread().getStackTrace()[1].getMethodName());
        logger.info("param:{}", param);

        param.trim();
        BasePageResponse pageResponse = new BasePageResponse();
        if (StringUtils.isNumeric(param) && param.length() < 30) {
            pageResponse = ogbTransactionService.findTibByHeight(pageNum, pageSize, Integer.parseInt(param));

        } else {
            pageResponse = ogbTransactionService.findTibByHash(pageNum, pageSize, param);
        }
        if (pageResponse == null) {
            return JsonResponse.serverError();
        } else {
            return JsonResponse.success(pageResponse).setCode(200).setMsg("成功");
        }

    }


}
